﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEET.CommonInterfaces.Compilers;
using LEET.LEETCommon.Model;
using Tedds_Tool.Instructions;
using LEET.StandardModules.Instructions.MouseActions;

namespace LEET.StandardModules.Instructions
{
    public abstract class MouseAction : Instruction
    {
        protected string sourceElement;
        protected string targetProcess;
        protected string targetElement;

        public MouseAction(string[] args) : base(args)
        {
            sourceElement = args[2];
            if (args.Length > 3)
            {
                targetProcess = args[3];
                targetElement = args[4];
            }
        }

        protected override string RenderedMethodName(int i)
        {
            return "_" + i + "_MOUSE_EVENT" + "()";
        }

        protected String MethodBody(String cursorAction)
        {
            return "                AutomationElement e = HierarchyFinder.GetElement(\"" + sourceElement + "\", Box.RootElement);\r\n" +
                "                MouseEvents." + cursorAction + "(e);\r\n" +
                "                return new AutomationActionResult();\r\n";
        }

        protected String OtherMethodBody(String cursorAction)
        {
            string pro = targetProcess.Substring(0, targetProcess.IndexOf('_'));
            string ret = "                AutomationElement sourceAE = HierarchyFinder.GetElement(\"" + sourceElement + "\", Box.RootElement);\r\n" +
                "                AutomationElement targetAE = HierarchyFinder.GetElement(\"" + targetElement + "\", Boxes[" + pro + "]);\r\n" +
                "                MouseEvents." + cursorAction + "(sourceAE, targetAE);\r\n" +
                "                return new AutomationActionResult();\r\n";
            return ret;
        }

        public static MouseAction apply(String[] args)
        {
            MouseAction ret = null;
            String type = args[1].Trim();
            switch (type)
            {
                case "LEFTCLICK": ret = new LeftClick(args); break;
                case "LEFT_DBL_CLICK": ret = new LeftDoubleClick(args); break;
                case "LEFTDOWN": ret = new LeftDown(args); break;
                case "LEFTUP": ret = new LeftUp(args); break;
                case "LEFTCLICKDRAG": ret = new LeftClickDrag(args); break;
                case "RIGHTCLICK": ret = new RightClick(args); break;
                case "RIGHT_DBL_CLICK": ret = new RightDoubleClick(args); break;
                case "RIGHTDOWN": ret = new RightDown(args); break;
                case "RIGHTUP": ret = new RightUp(args); break;
                case "RIGHTCLICKDRAG": ret = new RightClickDrag(args); break;
            }

            if (ret == null) throw new Exception("Unrecognized Action");

            return ret;
        }
    }
}
